﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SAFTCopyToDB.SAFTXML
{
    public static class SAFTXmlFileHelper
    {
        public static AuditFile LoadFile(string fileName)
        {
            return XmlSerializer.Deserialize<AuditFile>(fileName);
        }

        public static void MapHeader(Header h, EFCodeFirstSAFT context)
        {
            SAFTCopyToDB.Header header = new SAFTCopyToDB.Header();

            header.AuditFileVersion = h.AuditFileVersion;
            header.BusinessName = h.BusinessName;
            if (h.CompanyAddress != null)
            {
                header.AddressDetail = h.CompanyAddress.AddressDetail;
                header.BuildingNumber = h.CompanyAddress.BuildingNumber;
                header.City = h.CompanyAddress.City;
                header.Country = h.CompanyAddress.Country;
                header.PostalCode = h.CompanyAddress.PostalCode;
                header.Region = h.CompanyAddress.Region;
                header.StreetName = h.CompanyAddress.StreetName;
            }
            header.CompanyID = h.CompanyID;
            header.CompanyName = h.CompanyName;
            header.CurrencyCode = h.CurrencyCode;
            header.DateCreated = h.DateCreated;
            header.Email = h.Email;
            header.EndDate = h.EndDate;
            header.Fax = h.Fax;
            header.FiscalYear = h.FiscalYear;
            header.HeaderComment = h.HeaderComment;
            header.ProductCompanyTaxID = h.ProductCompanyTaxID;
            header.ProductID = h.ProductID;
            header.ProductVersion = h.ProductVersion;
            header.SoftwareCertificateNumber = h.SoftwareCertificateNumber;
            header.StartDate = h.StartDate;
            header.TaxAccountingBasis = h.TaxAccountingBasis.ToString();
            header.TaxEntity = h.TaxEntity;
            header.TaxRegistrationNumber = h.TaxRegistrationNumber;
            header.Telephone = h.Telephone;
            header.Website = h.Website;

            context.Header.Add(header);
            context.SaveChanges();
        }

        public static void MapSalesInvoices(SourceDocumentsSalesInvoices i, EFCodeFirstSAFT context, bool joinMode)
        {
            if (joinMode)
            {
                //ler os valores dos somatorios
                SAFTCopyToDB.SourceDocumentsSalesInvoices t =
                    (from si in context.SalesInvoices
                     select si).FirstOrDefault();
                if (t != null)
                {
                    //somar os valores por defeito
                    t.NumberOfEntries = (int.Parse(t.NumberOfEntries) + int.Parse(i.NumberOfEntries)).ToString();
                    t.TotalCredit += i.TotalCredit;
                    t.TotalDebit += i.TotalDebit;
                }
            }
            else
            {
                SAFTCopyToDB.SourceDocumentsSalesInvoices invoices = new SAFTCopyToDB.SourceDocumentsSalesInvoices();

                invoices.NumberOfEntries = i.NumberOfEntries;
                invoices.TotalCredit = i.TotalCredit;
                invoices.TotalDebit = i.TotalDebit;

                context.SalesInvoices.Add(invoices);
            }

            context.SaveChanges();
        }

        public static void MapInvoices(SourceDocumentsSalesInvoices i, EFCodeFirstSAFT context)
        {
            foreach (var fileInvoices in i.Invoice)
            {
                SAFTCopyToDB.SourceDocumentsSalesInvoicesInvoice newInvoice = new SAFTCopyToDB.SourceDocumentsSalesInvoicesInvoice();
                newInvoice.CustomerID = fileInvoices.CustomerID;
                if (fileInvoices.DocumentTotals != null)
                {
                    newInvoice.GrossTotal = fileInvoices.DocumentTotals.GrossTotal;
                    newInvoice.NetTotal = fileInvoices.DocumentTotals.NetTotal;
                    newInvoice.TaxPayable = fileInvoices.DocumentTotals.TaxPayable;
                }
                newInvoice.Hash = fileInvoices.Hash;
                newInvoice.HashControl = fileInvoices.HashControl;
                newInvoice.InvoiceDate = fileInvoices.InvoiceDate;
                newInvoice.InvoiceNo = fileInvoices.InvoiceNo;
                newInvoice.Period = fileInvoices.Period;
                newInvoice.SelfBillingIndicator = fileInvoices.SelfBillingIndicator;
                newInvoice.SystemEntryDate = fileInvoices.SystemEntryDate;
                newInvoice.TransactionID = fileInvoices.TransactionID;
                newInvoice.InvoiceStatus = fileInvoices.InvoiceStatus.ToString();
                newInvoice.InvoiceType = fileInvoices.InvoiceType.ToString();

                #region Lines
                foreach (var fileLine in fileInvoices.Line)
                {
                    SAFTCopyToDB.SourceDocumentsSalesInvoicesInvoiceLine newLine = new SAFTCopyToDB.SourceDocumentsSalesInvoicesInvoiceLine();
                    newLine.Invoice = newInvoice;

                    newLine.CreditAmount = fileLine.ItemElementName == ItemChoiceType2.CreditAmount ? fileLine.Item : 0;
                    newLine.DebitAmount = fileLine.ItemElementName == ItemChoiceType2.DebitAmount ? fileLine.Item : 0;
                    newLine.Description = fileLine.Description;
                    newLine.LineNumber = fileLine.LineNumber;
                    newLine.ProductCode = fileLine.ProductCode;
                    newLine.ProductDescription = fileLine.ProductDescription;
                    newLine.Quantity = fileLine.Quantity;
                    newLine.SettlementAmount = fileLine.SettlementAmount;
                    if (fileLine.Tax != null)
                    {
                        newLine.Tax = new SAFTCopyToDB.Tax();
                        newLine.Tax.TaxCode = fileLine.Tax.TaxCode;
                        newLine.Tax.TaxCountryRegion = fileLine.Tax.TaxCountryRegion;
                        newLine.Tax.TaxAmount = fileLine.Tax.ItemElementName == ItemChoiceType3.TaxAmount ? fileLine.Tax.Item : 0;
                        newLine.Tax.TaxPercentage = fileLine.Tax.ItemElementName == ItemChoiceType3.TaxPercentage ? fileLine.Tax.Item : 0;
                        newLine.Tax.TaxType = fileLine.Tax.TaxType.ToString();
                    }
                    newLine.TaxExemptionReason = fileLine.TaxExemptionReason;
                    newLine.TaxPointDate = fileLine.TaxPointDate;
                    newLine.UnitOfMeasure = fileLine.UnitOfMeasure;
                    newLine.UnitPrice = fileLine.UnitPrice;

                    context.InvoiceLines.Add(newLine);
                }
                #endregion Lines

                context.Invoices.Add(newInvoice);
                context.SaveChanges();
            }
        }
    }
}
